﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Group ManagementObject to Group Class
    /// </summary>
    public static class GroupTranslate
    {
        /// <summary>
        /// Convert Group ManagementObject to Group Class
        /// </summary>
        /// <param name="clsGrp">Group class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>Group class with properties converted</returns>
        public static GroupProperty Convert(this GroupProperty clsGrp, ManagementObject queryObj)
        {
            clsGrp.AntiAffinityClassNames = queryObj.PropertyValue<String[]>("AntiAffinityClassNames");
            clsGrp.AutoFailbackType = queryObj.PropertyValue<UInt32>("AutoFailbackType");
            clsGrp.Caption = queryObj.PropertyValue<String>("Caption");
            clsGrp.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristicsGroup>("Characteristics", CharacteristicsGroup.Unknown);
            //clsGrp.DefaultOwner = queryObj.PropertyValue<UInt32?>("DefaultOwner");
            clsGrp.Description = queryObj.PropertyValue<String>("Description");
            clsGrp.FailbackWindowEnd = queryObj.PropertyValue<UInt32>("FailbackWindowEnd");
            clsGrp.FailbackWindowStart = queryObj.PropertyValue<UInt32>("FailbackWindowStart");
            clsGrp.FailoverPeriod = queryObj.PropertyValue<UInt32>("FailoverPeriod");
            clsGrp.FailoverThreshold = queryObj.PropertyValue<UInt32>("FailoverThreshold");
            clsGrp.Flags = queryObj.PropertyValue<UInt32>("Flags");
            //clsGrp.GroupType = queryObj.PropertyEnum<UInt32, GroupType>("GroupType", GroupType.Unknown);
            clsGrp.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsGrp.InternalState = queryObj.PropertyValue<String>("InternalState");
            clsGrp.Name = queryObj.PropertyValue<String>("Name");
            //clsGrp.Priority = queryObj.PropertyValue<UInt32?>("Priority");
            clsGrp.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsGrp.PersistentState = queryObj.PropertyValue<bool>("PersistentState");
            clsGrp.State = queryObj.PropertyEnum<UInt32, StateGroupResources>("State", StateGroupResources.StateUnknown);
            clsGrp.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            return clsGrp;
        }

        ///<summary>
        /// Convert Group class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsGrp">Group class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this GroupProperty clsGrp, WindowsOperationSystem opSystem)
        {
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsGrp.Instance["AntiAffinityClassNames"] = clsGrp.AntiAffinityClassNames;
            }
            clsGrp.Instance["AutoFailbackType"] = clsGrp.AutoFailbackType;
            clsGrp.Instance["Characteristics"] = (UInt32)clsGrp.Characteristics;
            //if (opSystem == WindowsOperationSystem.Windows2008R2)
            //{
            //    clsGrp.Instance["DefaultOwner"] = clsGrp.DefaultOwner;
            //}
            clsGrp.Instance["Description"] = clsGrp.Description;
            clsGrp.Instance["FailbackWindowEnd"] = clsGrp.FailbackWindowEnd;
            clsGrp.Instance["FailbackWindowStart"] = clsGrp.FailbackWindowStart;
            clsGrp.Instance["FailoverPeriod"] = clsGrp.FailoverPeriod;
            clsGrp.Instance["FailoverThreshold"] = clsGrp.FailoverThreshold;
            clsGrp.Instance["Flags"] = clsGrp.Flags;
            //if (opSystem == WindowsOperationSystem.Windows2008R2)
            //{
            //    clsGrp.Instance["GroupType"] = (UInt32)clsGrp.GroupType;
            //}
            clsGrp.Instance["PersistentState"] = clsGrp.PersistentState;
            //if (opSystem == WindowsOperationSystem.Windows2008R2)
            //{
            //    clsGrp.Instance["Priority"] = clsGrp.Priority;
            //}
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsGrp.Instance["PrivateProperties"] = clsGrp.PrivateProperties.ConvertToManagemenBasetObject();
            }
            clsGrp.Instance["State"] = (UInt32)clsGrp.State;
            clsGrp.Instance.Put();
        }
    }
}
